//获取应用实例
const app = getApp();

Page({
    data: {
        wuliao_list: [],
        now_page: 0,
        list_total: 0,
        keywords: '',
        limit: 50,
        table_head: [
            {name: '物料名称', field: 'material_name', is_checked: 1, width: 350},
            {name: '供应商', field: 'supply_name', is_checked: 1, width: 200},
            {name: '颜色', field: 'material_color', is_checked: 1, width: 150},
            {name: '单价', field: 'price', is_checked: 1, width: 150},
            {name: '单位', field: 'unit', is_checked: 1, width: 150},
            {name: '幅宽', field: 'cloth_width', is_checked: 1, width: 180},
            {name: '克重', field: 'gram_weight', is_checked: 1, width: 180},
        ],
        table_scroll_left: 0,
        left_head_style: "",
        table_head_style: "",
        table_content_style: "",
        sel_list: [],
        supply_id: 0,
        sel_table_head: [
            {name: '物料名称', field: 'material_name', is_checked: 1, width: 350},
            {name: '供应商', field: 'supply_name', is_checked: 0, width: 200},
            {name: '颜色', field: 'material_color', is_checked: 0, width: 120},
            // {name: '采购数量', field: 'confirm_num', is_checked: 1, width: 200},
            // {name: '预计交期', field: 'dead_line', is_checked: 1, width: 200},
            {name: '单位', field: 'unit', is_checked: 1, width: 150},
            {name: '单价', field: 'price', is_checked: 1, width: 150},
            // {name: '用途', field: 'material_use', is_checked: 1, width: 200},
            // {name: '成品简码', field: 'template_no', is_checked: 1, width: 200},
            // {name: '单件用量', field: 'single_piece', is_checked: 1, width: 200},
            // {name: '缩率', field: 'no_use_rate', is_checked: 1, width: 200},

            {name: '幅宽', field: 'cloth_width', is_checked: 1, width: 200},
            {name: '克重', field: 'gram_weight', is_checked: 1, width: 200},

            // {name: '面料等级', field: 'material_grade', is_checked: 1, width: 200},
            // {name: '等级分类', field: 'grade_category', is_checked: 1, width: 200},
            // {name: '口袋布', field: 'pocketing', is_checked: 1, width: 200},
            // {name: '外发工厂', field: 'outgoing_factory', is_checked: 1, width: 200},
            {name: '物料图', field: 'temp_imgs', is_checked: 1, width: 200},
            // {name: '备注', field: 'remark', is_checked: 1, width: 200},
        ],
    },

    onLoad: function (options) {
        // 设置标题
        wx.setNavigationBarTitle({
            title: '选择物料'
        });
        // 接收传递过来的数据
        var __this = this;
        var eventChannel = this.getOpenerEventChannel();
        // 监听 acceptDataFromOpenerPage 事件，获取上一页面通过 eventChannel 传送到当前页面的数据
        eventChannel.on('acceptDataFromOpenerPage', function (data) {
            console.log(data)
            __this.setData({
                sel_list: data.bom_list,
                supply_id: data.supply_id,
            })
            // 获取物料
            __this.get_wuliao_list();
        });
    },

    // 页面触底时执行
    onReachBottom: function () {
        this.get_wuliao_list();
    },

    // 监听下拉[刷新]
    onPullDownRefresh: function () {
        this.setData({
            wuliao_list: [],
            now_page: 0,
            list_total: 0,
            keywords: '',
        });
        // 获取列表
        this.get_wuliao_list();
        // 停止下拉
        setTimeout(() => {
            wx.stopPullDownRefresh();
        }, 1000)
    },

    // 获取物料列表
    get_wuliao_list: function (is_search = 0) {
        var __this = this;
        // 验证是否超出最大页码
        if ((this.data.now_page + 1) > this.data.last_page) {
            return false;
        }
        // 请求参数
        var req_data = {
            page: this.data.now_page + 1,
            limit: this.data.limit,
            supply_id: this.data.supply_id,
            quick_search: this.data.keywords
        };
        // 发起请求
        var result = app.request_api('admin/material/getMaterialSku', req_data, true, 'GET');
        result.then(res => {
            console.log(res)
            if (is_search == 1) {
                this.setData({
                    wuliao_list: [],
                    now_page: 0,
                    list_total: 0
                })
            }
            if (res.list.length > 0) {
                res.list.forEach(function (item, index) {
                    res.list[index].is_checked = 0;
                    // if ((__this.data.sel_list.findIndex(obj => obj.material_sku_id === item.material_sku_id)) > -1) {
                    //     res.list[index].is_checked = 1;
                    // }
                });
            }
            this.setData({
                wuliao_list: this.data.wuliao_list.concat(res.list),
                list_total: res.total,
                now_page: this.data.now_page + 1,
            });
        })
    },

    // 当表格滚动时
    tr_boxs_scroll: function (e) {
        // if (app.globalData.platform == 'windows' || app.globalData.platform == 'mac' || app.globalData.platform == 'devtools') {
        //     this.setData({
        //         table_scroll_left: e.detail.scrollLeft
        //     })
        // }
    },

    // 表格滑动结束
    tr_boxs_scroll_end: function (e) {
        var __this = this;
        setTimeout(function () {
            __this.setData({
                table_scroll_left: e.detail.scrollLeft
            });
        }, 180);
    },

    // 当滑动到最右边时
    tr_boxs_bindscrolltolower: function (e) {
        var __this = this;
        // if (e.detail.direction == 'right') {
        //     setTimeout(function () {
        //         __this.setData({
        //             table_scroll_left: e.currentTarget.dataset.offsetLeft
        //         });
        //     }, 180);
        // }
    },
  // 预览图片
  PreviewImg: function (events) {
      var pic_arr = events.currentTarget.dataset.pic;
      if (pic_arr.length == 0) {
          return false;
      }
      // 调用
      wx.previewImage({
          current: pic_arr[0],
          urls: pic_arr
      })
    },
    // 当选择物料时
    materialChange: function (e) {
        console.log(e);
        var index = e.currentTarget.dataset.index;
        var choose_index = this.data.sel_list.findIndex(item => item.material_sku_id === this.data.wuliao_list[index].material_sku_id);
        // console.log(choose_index)
        // console.log(e.detail.value.length)
        if (e.detail.value.length > 0) {
            // if (choose_index >= 0) {
            //     wx.showToast({
            //         title: '已经选择过此物料了',
            //         icon: 'none',
            //         duration: 1500,
            //     });
            //     return false;
            // }
            // 选中
            // this.data.sel_list.push(this.data.wuliao_list[index]);
            var choose_item = this.data.wuliao_list[index];

            let new_list = []
            for(var k in choose_item.total_bom){
              new_list.push(choose_item.total_bom[k])
            }
            this.data.sel_list.push({
                material_id: choose_item.material_id,
                material_sku_id: choose_item.material_sku_id,
                material_name: choose_item.material_name,
                supply_name: choose_item.supply_name,
                material_color: choose_item.material_color,
                price: choose_item.price,
                dead_line: '',
                purchase_bom_detail_id: 0,
                confirm_num: '',
                material_use: choose_item.material_use,
                single_piece: choose_item.single_piece,
                template_no: choose_item.template_no,
                unit: choose_item.unit,
                cloth_width:choose_item.cloth_width,
                gram_weight:choose_item.gram_weight,
                remark: '',
                pro_list:new_list,
                pro_list_:choose_item.total_bom,
                temp_imgs:choose_item.temp_imgs,
                template_imgs:choose_item.template_imgs,

                template_retail_price:choose_item.template_retail_price,
                template_pro_cost:choose_item.template_pro_cost,
                template_order_num:choose_item.template_order_num,

                fileList:choose_item.fileList,
                is_close_phot:choose_item.is_close_phot,
                pocketing:choose_item.pocketing,
                no_use_rate:choose_item.no_use_rate,
                grade_category:choose_item.grade_category,
                material_grade:choose_item.material_grade,
                outgoing_factory:choose_item.outgoing_factory,
            });
            this.data.wuliao_list[index].is_checked = 1;
        } else {
            // 取消选中
            if (choose_index >= 0) {
                this.data.sel_list.splice(choose_index, 1);
                this.data.wuliao_list[index].is_checked = 0;
            }
        }
        this.setData({
            sel_list: this.data.sel_list,
            wuliao_list: this.data.wuliao_list
        });
    },

    // 搜索
    GoSearch: function (event) {
        var keywords = event.detail.value.trim();
        this.setData({
            keywords: keywords,
            wuliao_list: [],
            list_total: 0,
            now_page: 0,
        });
        this.get_wuliao_list(1);
    },

    // 取消选择
    chooseDel: function (e) {
        var index = e.currentTarget.dataset.index;
        // 将此条数据删除并将对应的取消选择
        var choose_index = this.data.wuliao_list.findIndex(obj => obj.material_sku_id === this.data.sel_list[index].material_sku_id);
        if (choose_index > -1) {
            this.data.wuliao_list[choose_index].is_checked = 0;
        }
        this.data.sel_list.splice(index, 1);
        // 重置data
        this.setData({
            sel_list: this.data.sel_list,
            wuliao_list: this.data.wuliao_list
        });
    },

    // 当点击确定选择时
    chooseConfirm: function (e) {
        var __this = this;
        // 向上一级页面传递参数
        const eventChannel = __this.getOpenerEventChannel();
        console.log('sel_list',__this.data.sel_list)
        eventChannel.emit('changeData', __this.data.sel_list);
        // 返回上一页
        wx.navigateBack();
    },

    // 当输入时
    formInputChange: function (e) {
        console.log(e)
        var index = e.currentTarget.dataset.index;
        var field = e.currentTarget.dataset.field;
        this.data.sel_list[index][field] = e.detail.value.trim();
        this.setData({
            sel_list: this.data.sel_list
        });
        console.log(this.data.sel_list)
    },

    // 选择交期时
    bindDeadLineChange: function (e) {
        console.log(e);
        var index = e.currentTarget.dataset.index;
        var field = e.currentTarget.dataset.field;
        this.data.sel_list[index][field] = e.detail.value;
        this.setData({
            sel_list: this.data.sel_list
        })
    },

})
